// utils/websocket/handlers/invite.js

import { MESSAGE_TYPES } from '../messageTypes';

export default function handleInviteMessage(message) {
  console.log('收到邀请进群消息:', message);

  const content = message.message;
  const groupIdMatch = /群ID：(\d+)/.exec(content);
  const invitedUserIdMatch = /邀请用户ID：(\d+)/.exec(content);

  const groupId = groupIdMatch ? parseInt(groupIdMatch[1], 10) : null;
  const invitedUserId = invitedUserIdMatch ? parseInt(invitedUserIdMatch[1], 10) : null;

  if (!groupId || !invitedUserId) {
    uni.showToast({ title: '消息格式错误', icon: 'none' });
    return;
  }

  uni.$emit('websocket-invite', {
    type: MESSAGE_TYPES.INVITE,
    groupId,
    invitedUserId,
    raw: message
  });

  uni.showToast({
    title: `收到进群邀请：群 ${groupId}`,
    icon: 'none',
    duration: 3000
  });
}